import {
    GET_ALL_ITEM,
    DEL_TODO_ITEM,
    CHANGE_TODO_ITEM,
    ADD_TODO_ITEM,
    REMOVE_FINISHED_TODO_ITEM,
    SELECT_ALL_TODO_ITEM
} from './actionTypes'
import { getTodoList } from '../api/index'

// 获取所有记录
export const getAllItemAction = () => {
    return (dispatch) => {
        getTodoList().then(res => {
            if (res.data.success_code === 200) {
                let todos = res.data.message;
                dispatch({
                    type: GET_ALL_ITEM,
                    todos
                });
            }
        });
    }
}

// 删除一条记录
export const getDelItemAction = (todoId) => ({
    type: DEL_TODO_ITEM,
    todoId
})

// 修改一条记录的状态
export const getChangeItemFinishedAction = (todoId, isFinished) => ({
    type: CHANGE_TODO_ITEM,
    todoId,
    isFinished
})

// 添加一条记录
export const getAddItemAction = (todo) => ({
    type: ADD_TODO_ITEM,
    todo
})

// 删除已完成的任务
export const getRemoveFinishedItemAction = () => ({
    type: REMOVE_FINISHED_TODO_ITEM
})

// 选中/取消所有
export const getSelectAllItemAction = (flag) => ({
    type: SELECT_ALL_TODO_ITEM,
    flag
})